﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Data.SqlClient;
using SmartLib;

public partial class Doanhnghiep_cong : System.Web.UI.Page
{
    public int i = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (cb_kcn.Text == "Không thuộc KCN")
        {
            CheckBox1.Checked = false;            
        }
        else
            CheckBox1.Checked = true;
    }

    protected void RadGrid_congxa_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        String id = item.GetDataKeyValue("Macong").ToString();

        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            SetMessage("congxa with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("congxa with ID " + id + " is updated!");
        }
    }
    protected void RadGrid_congxa_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        String id = dataItem.GetDataKeyValue("Macong").ToString();

        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("congxa with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("congxa with ID " + id + " is deleted!");
        }
    }

    private void DisplayMessage(string text)
    {
        RadGrid_congxa.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }

    private void SetMessage(string message)
    {
        gridMessage = message;
    }

    private string gridMessage = null;

    private void DisplayMessage(bool isError, string text)
    {
        Label label = (isError) ? this.Label1 : this.Label2;
        label.Text = text;
    }
    protected void RadGrid_congxa_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert(txtten.Text,Int32.Parse(cb_dn.SelectedValue),Int32.Parse(cb_kcn.SelectedValue),CheckBox1.Checked,Int32.Parse(txtX.Text),Int32.Parse(txtY.Text),RadEditor1.Text);
       // BindGridView();
    }
    private void insert(string Tencong, int Madnghiep, int Makcn, bool Thuockcn, int ToadoX, int ToadoY, string ghichu)
    {
       SqlConnection con = new SqlConnection("Data Source=C6E9961739FB407;Initial Catalog=QLMT;Integrated Security=True");
        //SqlConnection con = new SqlConnection(Connection.);
        string sqlStatement = string.Empty;

        sqlStatement = "INSERT INTO Congxa" +
                            "(Tencong,Madnghiep,Makcn,ThuocKCN,ToadoX,ToadoY,Ghichu)" +
                            "VALUES (@Tencong,@Madnghiep,@Makcn,@ThuocKCN,@ToadoX,@ToadoY,@Ghichu)";
      
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sqlStatement, con);          
            
            cmd.Parameters.AddWithValue("@Tencong",Tencong);
            cmd.Parameters.AddWithValue("@Madnghiep", Madnghiep);
            cmd.Parameters.AddWithValue("@Makcn", Makcn);            
            cmd.Parameters.AddWithValue("@ThuocKCN", Thuockcn);
            cmd.Parameters.AddWithValue("@ToadoX", ToadoX);
            cmd.Parameters.AddWithValue("@ToadoY", ToadoY);
            cmd.Parameters.AddWithValue("@Ghichu", ghichu);
           

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            con.Close();
        }
    }
    protected void cb_kcn_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        int x = Int32.Parse(cb_kcn.SelectedValue);
        //de d nap herper vo da
        string sql = "select dn.* from khucongnghiep kcn, doanhnghiep dn where	kcn.makcn = dn.makcn and kcn.Makcn = " + x.ToString();
        //thuc thi cau sql nay va tra ve datatable nhu the nao?
        DataTable myData = Helper.GetDataTable(sql);
        cb_dn.DataSource = myData;
        cb_dn.DataTextField = "TenDN";
        cb_dn.DataValueField = "Madnghiep";
        cb_dn.DataBind();
    }
    protected void refesh_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "Select * from Congxa";

    }
}
